<?php

namespace app\index\controller;

use app\index\controller\CommonController;
use app\index\model\Creditcate;
use app\index\model\Creditjob;
use app\index\model\Creditmoney;
use app\index\model\Creditmonth;
use app\index\model\Xinyong;
use app\index\model\Article;

/*
 * 信用贷
 */
class XinyongController extends CommonController
{
    public function index()
    {
        $param = input('param.');

        $search_money = isset($param['search_money']) ? $param['search_money'] : 0;
        $search_month = isset($param['search_month']) ? $param['search_month'] : 0;
        $search_cate = isset($param['search_cate']) ? $param['search_cate'] : 0;
        $search_job = isset($param['search_job']) ? $param['search_job'] : 0;
        $order_by = isset($param['order_by']) ? $param['order_by'] : 0;

        //贷款金额
        $creditmoneyList = Creditmoney::order('id asc')
            ->select();

        //贷款期限
        $creditmonthList = Creditmonth::order('id asc')
            ->select();

        //贷款类型
        $creditcateList = Creditcate::order('id asc')
            ->select();

        //职业身份
        $creditjobList = Creditjob::order('id asc')
            ->select();

        //贷款
        $where = [];
        if ($search_money != 0) {
            $where['money_min'] = ['<=', $search_money];
            $where['money_max'] = ['>=', $search_money];
        }
        if ($search_month != 0) {
            $where['month_min'] = ['<=', $search_month];
            $where['month_max'] = ['>=', $search_month];
        }
        if ($search_cate != 0) {
            $where['creditcate_id'] = ['<=', $search_cate];
        }
        if ($search_job != 0) {
            $where['creditjob_id'] = ['<=', $search_job];
        }

        $order = 'add_time desc';
        if ($order_by != 0) {
            if ($order_by == 1) {
                $order = 'money_min desc, money_max desc';
            } else if ($order_by == 2) {
                $order = 'month_min desc, month_max desc';
            } else if ($order_by == 3) {
                $order = 'lixi_min desc, lixi_max desc';
            } else if ($order_by == 4) {
                $order = 'speed_min desc, speed_max desc';
            }
        }

        $xinyongList = Xinyong::where($where)
            ->field('creditcate_id, creditjob_id, content', true)
            ->order($order)
            ->paginate(5);

        return view('index', [
            'menuIndex' => 2,
            'creditmoneyList' => $creditmoneyList,
            'creditmonthList' => $creditmonthList,
            'creditcateList' => $creditcateList,
            'creditjobList' => $creditjobList,
            'xinyongList' => $xinyongList,

            'search_money' => $search_money,
            'search_month' => $search_month,
            'search_cate' => $search_cate,
            'search_job' => $search_job,
            'order_by' => $order_by,
        ]);
    }

    public function content()
    {
        $id = input('param.id');

        $row = Xinyong::where('id', '=', $id)->find();

        //新手贷款
        $articleList = Article::where('artcate_id', '=', 4)
            ->field('id, title')
            ->order('add_time desc')
            ->limit(0, 8)
            ->select();

        return view('content', [
            'menuIndex' => 2,
            'row' => $row,
            'articleList' => $articleList,
        ]);
    }

}